<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" >
      <el-form-item label="检查记录ID" prop="inspectionRecordId">
        <el-input
          v-model="queryParams.inspectionRecordId"
          placeholder="请输入检查记录ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="照片名称" prop="photoName">
        <el-input
          v-model="queryParams.photoName"
          placeholder="请输入照片名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传时间" prop="uploadTime">
        <el-date-picker clearable
          v-model="queryParams.uploadTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择上传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上传人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入上传人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['nongjt:photo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongjt:photo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongjt:photo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['nongjt:photo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="photoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="缩略图" align="center" width="120">
        <template #default="scope">
          <image-preview :src="scope.row.photoUrl" :width="80" :height="80" />
        </template>
      </el-table-column>
      <el-table-column label="检查记录ID" align="center" prop="inspectionRecordId" />
      <el-table-column label="照片名称" align="center" prop="photoName" />
      <el-table-column label="照片大小" align="center" prop="photoSize" />
      <el-table-column label="上传时间" align="center" prop="uploadTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传人" align="center" prop="createBy" />
      <el-table-column label="照片备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handlePreview(scope.row)">预览</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nongjt:photo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nongjt:photo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检查记录照片关联对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="photoRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="检查记录ID" prop="inspectionRecordId">
              <el-input v-model="form.inspectionRecordId" placeholder="请输入检查记录ID" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="照片" prop="photoUrl">
              <image-upload v-model="form.photoUrl" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="照片名称" prop="photoName">
              <el-input v-model="form.photoName" placeholder="请输入照片名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="照片大小" prop="photoSize">
              <el-input v-model="form.photoSize" placeholder="请输入照片大小" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="上传时间" prop="uploadTime">
              <el-date-picker clearable
                v-model="form.uploadTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择上传时间"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="照片备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入照片备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    
    <!-- 照片预览对话框 -->
    <el-dialog :title="preview.title" v-model="preview.open" width="800px" append-to-body>
      <el-image
        :src="preview.photoUrl"
        :preview-src-list="[preview.photoUrl]"
        fit="contain"
        style="width: 100%; height: 60vh"
        preview-teleported>
      </el-image>
      <el-descriptions :column="2" border style="margin-top: 20px;">
        <el-descriptions-item label="检查记录ID">{{ preview.data.inspectionRecordId }}</el-descriptions-item>
        <el-descriptions-item label="照片名称">{{ preview.data.photoName }}</el-descriptions-item>
        <el-descriptions-item label="照片大小">{{ preview.data.photoSize }}</el-descriptions-item>
        <el-descriptions-item label="上传时间">{{ parseTime(preview.data.uploadTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="上传人">{{ preview.data.createBy }}</el-descriptions-item>
        <el-descriptions-item label="照片备注">{{ preview.data.remark }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="Photo">
import { listPhoto, getPhoto, delPhoto, addPhoto, updatePhoto } from "@/api/nongjt/photo"
import ImagePreview from '@/components/ImagePreview/index.vue'
import ImageUpload from '@/components/ImageUpload/index.vue'

const { proxy } = getCurrentInstance()

const photoList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inspectionRecordId: null,
    photoName: null,
    uploadTime: null,
    createBy: null,
  },
  rules: {
    inspectionRecordId: [
      { required: true, message: "检查记录ID不能为空", trigger: "blur" }
    ],
    photoUrl: [
      { required: true, message: "照片不能为空", trigger: "change" }
    ],
    uploadTime: [
      { required: true, message: "上传时间不能为空", trigger: "blur" }
    ],
  },
  preview: {
    open: false,
    title: "",
    photoUrl: "",
    data: {}
  }
})

const { queryParams, form, rules, preview } = toRefs(data)

/** 查询检查记录照片关联列表 */
function getList() {
  loading.value = true
  listPhoto(queryParams.value).then(response => {
    photoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inspectionRecordId: null,
    photoUrl: null,
    photoName: null,
    photoSize: null,
    uploadTime: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    remark: null
  }
  proxy.resetForm("photoRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加检查记录照片关联"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPhoto(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改检查记录照片关联"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["photoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePhoto(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPhoto(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除检查记录照片关联编号为"' + _ids + '"的数据项？').then(function() {
    return delPhoto(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nongjt/photo/export', {
    ...queryParams.value
  }, `photo_${new Date().getTime()}.xlsx`)
}

/** 预览按钮操作 */
function handlePreview(row) {
  data.preview.open = true
  data.preview.title = "照片预览"
  data.preview.photoUrl = row.photoUrl.startsWith('http') ? row.photoUrl : import.meta.env.VITE_APP_BASE_API + row.photoUrl
  data.preview.data = row
}

getList()
</script>